#ifndef ODBC_DETAILS_BUFFER_BASE_HPP
#define ODBC_DETAILS_BUFFER_BASE_HPP

#include "odbc/details/os_sql_wrapper.hpp"

namespace odbc { namespace details
{

/// \ingroup odbcpp
/// \private
/// \brief It implements the basic support and functionality of a buffer. 
/// \details 
///		Its only purpose is the reusability for the derived classes, in order to avoid writing repetitive code for 
///		buffer template class specializations.
///		\n
///		Since at this level of abstraction we can't make any difference between variable size buffer and fixed size 
///		buffer, the buffer base doesn't implement anything that has to do with memory management. The specialized 
///		classes should do that. It only deals with the length indicator needed by the ODBC API functions for passing the 
///		buffer size and the NULL flag of a value contained in the buffer.
/// \param OdbcTID the identifier of the ODBC type corresponding to the type to be mapped to the buffer. 
///		See odbc::odbc_types
/// \param SqlTID the identifier of the SQL type corresponding to the type to be mapped to the buffer.
///		See odbc::sql_types
template< odbc_types::type_enum OdbcTID, sql_types::type_enum SqlTID>
class buffer_base
{
public:
	/// \brief Aliases for the ODBC and SQL type id parameters.
	enum
	{
		odbc_type_id = OdbcTID,
		sql_type_id = SqlTID
	};

	typedef std::size_t size_type;

public:
	/// \brief It initializes the length indicator with no data.
	buffer_base()
		: length_indicator_( SQL_NULL_DATA)
	{
	}

	/// \brief It initializes the length indicator with the given size.
	buffer_base( size_type size)
		: length_indicator_( size)
	{
	}

	/// \brief It provides access to the length indicator, by address.
	/// \details
	///		It is mainly needed by some classes (like statements) when performing calls to ODBC API functions, which 
	///		require it.
	SQLINTEGER* length_indicator_ptr() { return &length_indicator_; }

	/// \brief It checks whether the buffer contains a null value or not.
	bool contains_null() const { return length_indicator_ == SQL_NULL_DATA; }

	/// \brief It marks the buffer as containing NULL data.
	void set_null() { length_indicator_ = SQL_NULL_DATA; }

protected:
	void set_length_indicator( size_type size)
	{
		length_indicator_ = size;
	}

private:
	SQLINTEGER length_indicator_;
};


} } // namespace details // namespace odbc

#endif // ODBC_DETAILS_BUFFER_BASE_HPP
